//
//  BKPermissionInterface.swift
//  dysaidao
//
//  Created by 王锦发 on 2021/10/6.
//  Copyright © 2021 王锦发. All rights reserved.
//

import UIKit
import AVFoundation
import Photos
import MediaPlayer

enum BKAuthorizationStatus {
    // 未知状态
    case unknown
    // 用户未选择
    case notDetermined
    // 用户没有权限
    case restricted
    // 拒绝
    case denied
    // 允许
    case authorized
    // 临时允许
    case provisional
    // 设备不支持
    case notSupport
    
    /// 是否可以访问
    var isAuthorized: Bool {
        return self == .authorized || self == .provisional
    }
    /// 是否不支持
    var isNotSupport: Bool {
        return self == .notSupport
    }
}

// MARK: - 权限协议
protocol BKPermissionInterface {
    /// 是否允许
    var isAuthorized: Bool { get }
    /// 是否拒绝
    var isDenied: Bool { get }
    /// 是否每次询问
    var isNotDetermined: Bool { get }
    /// 请求权限
    func request(completion callback: ((BKAuthorizationStatus) -> Void)?)
}

// MARK: - 相册
/// 相册权限
struct BKPhotoLibraryPermission: BKPermissionInterface {
    
    var isAuthorized: Bool {
        if #available(iOS 14, *) {
            return PHPhotoLibrary.authorizationStatus(for: .readWrite) == .authorized
        } else {
            return PHPhotoLibrary.authorizationStatus() == .authorized
        }
    }
    
    var isDenied: Bool {
        if #available(iOS 14, *) {
            return PHPhotoLibrary.authorizationStatus(for: .readWrite) == .denied
        } else {
            return PHPhotoLibrary.authorizationStatus() == .denied
        }
    }
    
    var isNotDetermined: Bool {
        if #available(iOS 14, *) {
            return PHPhotoLibrary.authorizationStatus(for: .readWrite) == .notDetermined
        } else {
            return PHPhotoLibrary.authorizationStatus() == .notDetermined
        }
    }
    
    func request(completion callback: ((BKAuthorizationStatus) -> Void)?) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            var authStatue: BKAuthorizationStatus = .unknown
            if #available(iOS 14, *) {
                switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
                case .authorized: authStatue = .authorized
                case .notDetermined: authStatue = .notDetermined
                case .restricted: authStatue = .restricted
                case .denied: authStatue = .denied
                default: authStatue = .unknown
                }
                if authStatue == .notDetermined {
                    PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
                        if status == .authorized {
                            authStatue = .authorized
                        } else if status == .denied {
                            authStatue = .denied
                        }
                        callback?(authStatue)
                    }
                } else {
                    callback?(authStatue)
                }
            } else {
                switch PHPhotoLibrary.authorizationStatus() {
                case .authorized: authStatue = .authorized
                case .notDetermined: authStatue = .notDetermined
                case .restricted: authStatue = .restricted
                case .denied: authStatue = .denied
                default: authStatue = .unknown
                }
                if authStatue == .notDetermined {
                    PHPhotoLibrary.requestAuthorization { status in
                        if status == .authorized {
                            authStatue = .authorized
                        } else if status == .denied {
                            authStatue = .denied
                        }
                        callback?(authStatue)
                    }
                } else {
                    callback?(authStatue)
                }
            }
        } else {
            callback?(.notSupport)
        }
    }
    
}

// MARK: - 相机
/// 相机权限
struct BKCameraPermission: BKPermissionInterface {
    
    var isAuthorized: Bool {
        return AVCaptureDevice.authorizationStatus(for: .video) == .authorized
    }
    
    var isDenied: Bool {
        return AVCaptureDevice.authorizationStatus(for: .video) == .denied
    }
    
    var isNotDetermined: Bool {
        return AVCaptureDevice.authorizationStatus(for: .video) == .notDetermined
    }
    
    func request(completion callback: ((BKAuthorizationStatus) -> Void)?) {
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            var authStatue: BKAuthorizationStatus = .unknown
            switch AVCaptureDevice.authorizationStatus(for: .video) {
            case .authorized: authStatue = .authorized
            case .notDetermined: authStatue = .notDetermined
            case .restricted: authStatue = .restricted
            case .denied: authStatue = .denied
            default: authStatue = .unknown
            }
            if authStatue == .notDetermined {
                AVCaptureDevice.requestAccess(for: .video) { authorized in
                    callback?(authorized ? .authorized : .denied)
                }
            } else {
                callback?(authStatue)
            }
        } else {
            callback?(.notSupport)
        }
    }
    
}
